/*
 * Powered By [Frank-Liz-Lee]
 * Copyright(C) 2012-2016 Liz Company
 * All rights reserved.
 */

package com.linln.modules.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.mapper.RoleMapper;
import com.linln.modules.system.domain.Role;
import java.util.List;
import java.util.Set;

import com.linln.modules.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Transactional(rollbackFor = Exception.class)
@Service
public class RoleServiceImpl  extends GenericService<Role, Long>  implements RoleService {

    private RoleMapper roleMapper;

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    /**
     * 通过主键查询实体对象
     * @param primaryKey
     * @return
     * @
     */
    public Role getByPK(Long primaryKey)  {
        return roleMapper.getByPK(primaryKey);
    }

    /**
     * 查询所有记录
     * @return
     * @
     */
    public List<Role> list()  {
        return roleMapper.list();
    }

    /**
     * 根据查询条件查询所有记录
     * @return
     * @
     */
    public List<Role> listByProperty(Role role) {
        return roleMapper.listByProperty(role);
    }


    /**
     * 根据主键删除记录
     * @param primaryKey
     * @return
     * @
     */
    public int deleteByPK(Long primaryKey)  {
        return roleMapper.deleteByPK(primaryKey);
    }

    /**
     * 根据多个主键删除记录
     * @param primaryKeys
     * @
     */
    public int deleteByPKeys(List<Long> primaryKeys)  {
        return roleMapper.deleteByPKeys(primaryKeys);
    }

    /**
     * 根据传入参数删除记录
     * @param role
     * @return
     * @
     */
    public int deleteByProperty(Role role)  {
        return roleMapper.deleteByProperty(role);
    }

    /**
     * 保存记录
     * @param role
     * @return
     * @
     */
    public int save(Role role)  {
        return roleMapper.save(role);
    }

    /**
     * 更新记录
     * @param role
     * @return
     * @
     */
    public int update(Role role) {
        return roleMapper.update(role);
    }

    /**
     * 根据条件查询记录条数
     * @param role
     * @return
     * @
     */
    public int findByCount(Role role)  {
        return roleMapper.findByCount(role);
    }

    /**
     * 根据查询条件查询分页记录
     * @return
     */
    public PageInfo<Role> findByPage(Integer pageNum, Integer pageSize, Role role) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList = roleMapper.listByProperty(role);
        if(null == roleList || roleList.size() == 0){
            return new Page<Role>().toPageInfo();
        }
        Page<Role> page =  (Page<Role>)roleList;
            return page.toPageInfo();
    }

    @Override
    public boolean existsUserOk(Long id) {
        List<Role> roleSet = roleMapper.findByUserId(id,StatusEnum.OK.getCode());
        return !CollectionUtils.isEmpty(roleSet);
    }


    @Override
    public boolean repeatByName(Role query) {
        Role temp = new Role();
        temp.setName(query.getName());
        List<Role> list = roleMapper.listByProperty(temp);
        if(CollectionUtils.isEmpty(list)){
            return false;
        }
        Role role = list.get(0);
        if(query.getId() != null){
            return role != null && role.getId().equals(query.getId());
        }else{
            return role != null;
        }
    }

    @Override
    public boolean updateStatus(StatusEnum statusEnum, List<Long> ids) {
        int result =  roleMapper.updateStatus(statusEnum.getCode(),ids);
        return result > 0;
    }

    @Override
    public List<Role> getListBySortOk() {
        Role temp =new Role();
        temp.setStatus(StatusEnum.OK.getCode());
        return roleMapper.listByProperty(temp);
    }

    @Override
    public List<Role> getUserOkRoleList(Long id) {
        return roleMapper.findByUserId(id,StatusEnum.OK.getCode());
    }


}
